package com.anycl.customer.servlet.customer;

import com.anycl.customer.exception.CustomerException;
import com.anycl.customer.service.CustomerService;
import com.anycl.customer.service.impl.CustomerServiceImpl;
import com.anycl.customer.vo.CustomerVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/customer/delete")
public class CustomerDeleteServlet extends HttpServlet {

    private CustomerService customerService;

    @Override
    public void init() throws ServletException {
        customerService = new CustomerServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        try {
            CustomerVo response = customerService.deleteCustomerById(Long.parseLong(id), req.getSession());
            resp.getWriter().write("客户信息删除成功，客户id：" + response.getId() + "，客户名字：" + response.getName());
        } catch (CustomerException e) {
            resp.getWriter().write("error: " + e.getMessage());
        }
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        if (method.equalsIgnoreCase("get")) {
            doGet(req, resp);
        } else {
            doPost(req, resp);
        }
    }

}
